new Vue({
    el:'#main',
    data:()=>({
        data:[],
        cond:{
            all:true,
            play:false,
            sign:false,
            receipt:false,
            del:false,
            searchText:'',
            page:1,
            limit:30
        },
        iscontent:2,
        iszwxd:2,
        condArr:[],
        cheval:[],
        allche:false,
        store:[false,false,false,false],
        order:false,//升序或降序
        sortname:'',
        auth_ids:$('#adminauth_ids').text(),
        isshow_search:false
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            if(this.cond.searchText != '') this.isshow_search = true
            $('.loding').show()
            $.ajax({
                url:'/admin/Buypo/list',
                type:'post',
                data:this.cond,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.data = data.data.data;
                    this.cheval=[]
                    this.data.forEach(element => {
                        this.cheval.push({
                            val:false,
                            id:element.id
                        })
                    });
                    this.cond.page = data.data.page
                    // this.page = data.data.page
                    if(data.data.size)
                    this.Resetnav(this.cond.page-0,data.data.size)
                    $('.loding').hide()
                }
            })
        },
        //切换状态
        changestatus(status){
            if(status=='iscontent'){
                if(this.iscontent==1){
                    this.iscontent=2;
                }else {
                    this.iscontent=1;
                }
            }else if(status=='iszwxd'){
                if(this.iszwxd==1){
                    this.iszwxd=2;
                }else {
                    this.iszwxd=1;
                }
            }else {
                // this.cond[status]!=this.cond[status];
                if(this.cond[status]){
                    this.cond[status] = false
                }else{
                    this.cond[status] = true
                }
                this.getdata();
            }
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.cond.limit;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.cond.page+'PG , '+this.cond.limit+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.cond.page=num
                        this.getdata()
                    }
                }
            });
        },
        printf(){//打印
            $('a').hide()
            $('.btn-group').hide()
            $("#printArea").jqprint();
            $('a').show()
            $('.btn-group') .show()
        },
        batch(urlstr){
            var data={buy_po_ids:[]}
            this.cheval.forEach(element => {
                if(element.val)
                data.buy_po_ids.push(element.id)
            });
            $.ajax({
                url:'/admin/buypo/'+urlstr,
                type:'post',
                data,
                success:data=>{
                    data = JSON.parse(data)
                    change_mes_type('.modal-body .alert',data.status)
                    $('.column .alert').show()
                    $('.column .alert p').html(data.msg)
                    if(data.status==1){
                        this.getdata()
                    }
                }
            }) 
        },
        changeall(){
            this.cheval.forEach(element=>{
                element.val= this.allche
            })
        },
        oneaction(urlstr,id){
            $.ajax({
                url:'/admin/buypo/'+urlstr,
                type:'post',
                data:{
                    buy_po_ids:[id]
                },
                success:data=>{
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    $('.column .alert').show()
                    $('.column .alert p').html(data.msg)
                    if(data.status==1){
                        this.getdata()
                    }
                }
            })  
        },
        storing(i_order,keyname){
            this.order=this.store[i_order]
            this.store[i_order]=!this.store[i_order]
            this.sortname=keyname
            this.data.sort(this.sortoneData)
        },
        sortoneData(a, b) { 
            if (this.order == 1) {
                return b[this.sortname] - a[this.sortname]
            } else {
                return a[this.sortname] - b[this.sortname]
            }
        },
        Money(value, num) {
            num = num > 0 && num <= 20 ? num : 2;
            value = parseFloat((value + "").replace(/[^\d\.-]/g, "")).toFixed(num) + ""; //将金额转成比如 123.45的字符串
            var valueArr = value.split(".")[0].split("") //将字符串的数变成数组
            const valueFloat = value.split(".")[1]; // 取到 小数点后的值
            let valueString = "";
            for (let i = 0; i < valueArr.length; i++) {
            valueString += valueArr[i] + ((i + 1) % 3 == 0 && (i + 1) != valueArr.length ? "," : ""); //循环 取数值并在每三位加个','
            }
            const money = valueString.split("").join("") + "." + valueFloat; //拼接上小数位
            return money
        },
        setStorage(){
            var storage=window.localStorage;
            storage.setItem("buypo_page", this.cond.page);
            storage.setItem('buypo_searchText',this.cond.searchText);
        },
        //关闭搜索内容
        shdow(){
            this.cond.searchText = ''
            this.isshow_search=false
            this.getdata()
        },
    },
    mounted(){
        if($('#formMenu').text() != 1){
            if(window.localStorage.getItem('buypo_page')){  
                this.cond.page=window.localStorage.getItem('buypo_page')
                this.cond.searchText=window.localStorage.getItem('buypo_searchText')
            }
        }
        window.localStorage.removeItem('buypo_page')
        window.localStorage.removeItem('buypo_searchText')

        if(this.cond.searchText != '') this.isshow_search = true

        this.getdata()
    },
    watch:{
        cheval:{
            handler(){
                if(this.cheval.every(element=>element.val) && this.cheval.length){
                    this.allche=true
                }else{
                    this.allche=false
                }
            },
            immediate:false,
            deep:true
        }
    }
})